package com.hopu.pyq.controller;

import com.hopu.pyq.entity.User;
import com.hopu.pyq.entity.Zan;
import com.hopu.pyq.service.UserService;
import com.hopu.pyq.service.ZanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 点赞控制器
 */
@RestController
public class ZanController {

    @Autowired
    private ZanService zanService;

    @Autowired
    private UserService userService;

    @PostMapping("clickZan")
    public ResponseEntity<String> clickZan(Integer userId,Integer pyqId){
        Zan zan = zanService.findZanByUserIdAndPyqId(userId, pyqId);
        if(zan != null){
            return ResponseEntity.ok("already");
        }
        //加点赞
        zanService.addZan(new Zan(0,userId,pyqId));
        return ResponseEntity.ok("ok");
    }

    @GetMapping("findPyqUsers")
    public ResponseEntity<List<User>> findZanUsers(Integer pyqId){
        //把所有点赞人查出来
        List<User> users = userService.findUsersByPyqId(pyqId);
        return ResponseEntity.ok(users);
    }
}
